%Script to show histogram

cateName = cell(10);
cateName{1} = 'White';
cateName{2} = 'Black';
cateName{3} = 'Red';
cateName{4} = 'Yellow';
cateName{5} = 'Dark Blue';
cateName{6} = 'Green';
cateName{7} = 'Gray';
cateName{8} = 'Silver';
cateName{9} = 'Light Blue';
cateName{10}= 'Champion';



dataPath = '../CarColorSamples';
labelVec=[];
load('model2.mat');
gmm=model;
for iColor = 0:9
    dataCell = cell(0);
    iSample= 1;
    color_path = fullfile(dataPath, num2str(iColor));
    fileList = dir(fullfile(color_path,'*.jpg'));
    vecImgList = [];
    for iFile= 1:16:length(fileList);
      img=imresize(imread(fullfile(color_path,fileList(iFile).name)),[NaN,50]);
      vecImg = reshape(im2double(img),[],3);
      vecImgList = [vecImgList; vecImg];
    end
    vecImg=im2double(vecImgList);
    %{
    scatter3(vecImg(:,1),vecImg(:,2),vecImg(:,3),5+ones(size(vecImg,1),1),vecImg,'filled')
    xlabel('R Value');
    ylabel('G Value');
    zlabel('B Value');
    saveas(gcf,[cateName{iColor+1} '_before.fig']);
    view(-60,60);
    saveas(gcf,[cateName{iColor+1} '_view1_before.png']);
    view(40,35)
    saveas(gcf,[cateName{iColor+1} '_view2_before.png']);
    close;
    %}
    distMat = mahal(gmm,vecImg);
    ind1 = find(distMat(:,2)<4);
    ind2 = find(distMat(:,5)<4);
    idx = sort(unique([ind1;ind2]));
    idx = setdiff(1:size(vecImg,1),idx);
    vecImg = vecImg(idx,:);
    scatter3(vecImg(:,1),vecImg(:,2),vecImg(:,3),5+ones(length(idx),1),vecImg,'filled')
    xlabel('R Value');
    ylabel('G Value');
    zlabel('B Value');
    saveas(gcf,[cateName{iColor+1} '_after.fig']);
    view(-60,60);
    saveas(gcf,[cateName{iColor+1} '_view1_after.png']);
    view(40,35)
    saveas(gcf,[cateName{iColor+1} '_view2_after.png']);
    close all 
    
    
   disp(cateName{iColor+1});
end
